import Vue from 'vue'

import { save, unsave, savelist, shopcartlist, shopcartclear, shopcartadd, shopcartsub,addresslist,addressadd,addressedit,addressdel } from "../api";

//1.导入vuex
import Vuex from 'vuex'
//2.注册vuex
Vue.use(Vuex)
//3.实例化并导出vuex实例
export default new Vuex.Store({
  state: { //负责存储数据
    savelist: [],
    savestate: '',
    cartlist: [],
    sumprice:0,
    addresslist:[],
  },
  getters: { //负责定义计算方法(类似于组件中的计算属性)
    sum(state){
      state.cartlist.forEach((item) => {
        state.sumprice += item.count * item.min_price;
      });
      return state.sumprice;
    }
  },
  mutations: { //负责定义同步方法( 可以直接修改state中的数据 )
    //收藏=========================
    getsavelist(state, payload) {
      savelist(payload).then((res) => {
        state.savelist = res.data.list;
        // console.log(state.savelist);
      })
    },
    save(state, payload) {
      save(payload).then((res) => {
        this.state.savestate = res.data.msg;
        // console.log(this.state.savestate);
        // console.log(res.data);
        savelist({ userid: localStorage.getItem('id') }).then((res) => {
          state.savelist = res.data.list;
          // console.log(state.savelist);
        })
      })
    },
    unsave(state, payload) {
      unsave(payload).then((res) => {
        this.state.savestate = res.data.msg;
        // console.log(res.data);
        savelist({ userid: localStorage.getItem('id') }).then((res) => {
          state.savelist = res.data.list;
        })
      })
    },
    //购物车=======================
    getcartlist(state, payload) {
      shopcartlist(payload).then((res) => {
        state.cartlist = res.data.list;
        // console.log(res.data);
      })
    },
    clearcartlist(state, payload) {
      shopcartclear(payload).then((res) => {
        // console.log(res.data);
        state.cartlist = res.data.list;
      })
    },
    shopcartadd(state, payload) {
      shopcartadd(payload).then((res) => {
        // console.log(res.data);
        state.cartlist = res.data.list;
      })
    },
    shopcartsub(state, payload) {
      shopcartsub(payload).then((res) => {
        // console.log(res.data);
        state.cartlist = res.data.list;
      })
    },
    //收货地址======================
    addresslist(state, payload) {
      addresslist(payload).then((res) => {
        state.addresslist = res.data.list;
        state.addresslist = state.addresslist.map(item=>{
          // console.log(item);
          return {name:item.name,tel:item.phone,address:item.address,addressid:item.id}
        })
        // console.log(state.addresslist);
      })
    },
    addressadd(state, payload) {
      addressadd(payload).then((res) => {
        // console.log(res.data);
        state.addresslist = res.data.list;
      })
    },
    addressedit(state, payload) {
      addressedit(payload).then((res) => {
        console.log(res.data);
      })
    },
    addressdel(state, payload) {
      addressdel(payload).then((res) => {
        // console.log(res.data);
        state.addresslist = res.data.list;
      })
    },
  },
  actions: { //负责定义异步方法( 内部一般书写异步代码( 定时器,ajax ) 不可以直接修改state中的数据 )

  },
  modules: { //实现state的模块化管理

  },
  plugins: [] //配合其他插件管理state中的数据
})
